#include "loginwidget.h"
#include "ui_loginwidget.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QPainter>
#include <QPixmap>
#include <QDebug>

LoginWidget::LoginWidget(QWidget *parent) : QWidget(parent) {
    // 设置固定窗口大小
    this->setFixedSize(800, 600);

    // 设置背景颜色为淡灰色
    this->setStyleSheet("background-color: #f5f5f5;");

    // 欢迎标题
    QLabel *title = new QLabel("欢迎使用医院预约挂号系统", this);
    title->setAlignment(Qt::AlignCenter);
    title->setStyleSheet("font-size: 24px; font-weight: bold; color: #4A90E2; margin-bottom: 30px;");

    // 账号输入框
    usernameInput = new QLineEdit(this);
    usernameInput->setPlaceholderText("请输入账号");
    usernameInput->setStyleSheet("padding: 10px; font-size: 16px; border: 1px solid #ccc; border-radius: 10px; background-color: #fff;");
    usernameInput->setText("doctor1");

    // 密码输入框
    passwordInput = new QLineEdit(this);
    passwordInput->setPlaceholderText("请输入密码");
    passwordInput->setText("password3");
    passwordInput->setEchoMode(QLineEdit::Password);
    passwordInput->setStyleSheet("padding: 10px; font-size: 16px; border: 1px solid #ccc; border-radius: 10px; background-color: #fff;");

    // 角色选择框
    roleSelect = new QComboBox(this);
    roleSelect->addItem("用户", "Users");
    roleSelect->addItem("医生", "Doctors");
    roleSelect->addItem("管理员", "Admins");
    roleSelect->setStyleSheet("padding: 10px; font-size: 16px; border: 1px solid #ccc; border-radius: 10px; background-color: #fff;");

    // 登录按钮
    QPushButton *loginButton = new QPushButton("登录", this);
    loginButton->setStyleSheet(
        "background-color: #4A90E2; color: white; font-size: 18px; padding: 15px; border-radius: 10px; margin-top: 20px;");

    // 注册按钮
    QPushButton *registerButton = new QPushButton("注册", this);
    registerButton->setStyleSheet(
        "background-color: #f5a623; color: white; font-size: 18px; padding: 15px; border-radius: 10px; margin-top: 10px;");

    // 表单布局
    QFormLayout *formLayout = new QFormLayout;
    formLayout->addRow(usernameInput);
    formLayout->addRow(passwordInput);
    formLayout->addRow(roleSelect);
    formLayout->addWidget(loginButton);
    formLayout->addWidget(registerButton);

    // 总体布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(title);
    mainLayout->addLayout(formLayout);
    mainLayout->setAlignment(Qt::AlignCenter);

    // 连接登录按钮的点击信号到相应的槽函数
    connect(loginButton, &QPushButton::clicked, this, &LoginWidget::onLoginClicked);
    // 连接注册按钮的点击信号到相应的槽函数
    connect(registerButton, &QPushButton::clicked, this, &LoginWidget::onRegisterClicked);

    registerWidget = new Register();

    connect(registerWidget, &Register::finish, this, &LoginWidget::showLoginWidget);

}

void LoginWidget::onLoginClicked()
{
    // 从用户名输入框获取输入的用户名
    QString username = usernameInput->text();

    // 从密码输入框获取输入的密码
    QString password = passwordInput->text();

    QString selectedData = roleSelect->currentData().toString(); // 获取用户数据
    //qDebug() << selectedData;

    QString sql = QString("select *from %3 where Username = '%1' and Password = '%2'").arg(username).arg(password).arg(selectedData);

    //判断当前用户角色
    this->role = roleSelect->itemData(roleSelect->currentIndex()).toString();
    qDebug()<<"**************************************************";
    qDebug()<<role;
    qDebug()<<"**************************************************";

    if(role=="Users"){

        tcp = new ClientTcp(USERS_DATA);
        tcp->sendMsg(sql);
        connect(tcp->socket,&QTcpSocket::readyRead,[=]() {
            if(tcp->getUsersData().isEmpty()){
                //                QMessageBox::warning(this, "登录失败", "用户名或密码错误！");
            } else{
                this->currentID = tcp->getUsersData()[0]["UserID"].toInt();
                QMessageBox::information(this, "登录成功", "欢迎回来，" + username + "！");
                for (const QJsonObject &item : tcp->getUsersData()) {
                    this->home = nullptr;
                    this->home = new HomePage(currentID,role);
                    connect(home,&HomePage::userLoggedOut,this,&LoginWidget::show);
                    qDebug() << "#######################################################################";
                    qDebug() << "login to home" << "currentID:" << currentID << "role: " << role ;
                    qDebug() << "#######################################################################";
                    this->close();
                    home->show();
                }
            }
        });
    }else if(role=="Doctors"){

            tcp = new ClientTcp(DOCTORS_DATA);
            tcp->sendMsg(sql);
            connect(tcp->socket,&QTcpSocket::readyRead,[=]() {
                if(tcp->getDoctorsData().isEmpty()){
                                    QMessageBox::warning(this, "登录失败", "用户名或密码错误！");
                } else{
                    this->currentID = tcp->getDoctorsData()[0]["DoctorID"].toInt();
                    QMessageBox::information(this, "登录成功", "欢迎回来，" + username + "！");
                    for (const QJsonObject &item : tcp->getDoctorsData()) {
                      this->doctorfunctionWidget = nullptr;
                        this->doctorfunctionWidget = new doctorFunctionWidget(currentID,role);
                         connect(doctorfunctionWidget,&doctorFunctionWidget::doctorLoggedOut,this,&LoginWidget::show);

                         this->close();
                        doctorfunctionWidget->show();
                    }
                }
            });
        }
    }


void LoginWidget::showLoginWidget()
{
//>>>>>>> project-branch
    registerWidget->close();
    this->show();
}

void LoginWidget::onRegisterClicked() {
    // 显示注册窗口
    registerWidget = NULL;
    registerWidget = new Register();
    registerWidget->setModal(true);
    registerWidget->exec();  // 显示模态窗口
}

void LoginWidget::paintEvent(QPaintEvent *event) {
    Q_UNUSED(event); // 防止未使用参数警告
}
